﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class SystemNode : Form
    {
        public SystemNode()
        {
            InitializeComponent();
            this.Opacity = 0;
        }

        private void SystemNode_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            usersystemnode system = new usersystemnode();
            DataTable dt = system.GetList("").Tables[0];
            dgvsystemnode.DataSource = dt;
            dgvsystemnode.AllowUserToAddRows = false;
            operatre();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        private void operatre()
        {
            comboBox1.Text = "所有操作员";
            comboBox1.Items.Add("所有操作员");
            operatorInfo info = new operatorInfo();
            DataTable dt = info.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (!comboBox1.Items.Contains(dt.Rows[i]["username"].ToString()))
                {
                    comboBox1.Items.Add(dt.Rows[i]["username"].ToString());
                }
            }

        }
        private void btnSelect_Click(object sender, EventArgs e)
        {
            string time = dateTimePicker1.Text + " " + dateTimePicker2.Text;
            string time1 = dateTimePicker3.Text + " " + dateTimePicker4.Text;
            usersystemnode system = new usersystemnode();
            if (comboBox1.Text == "所有操作员")
            {
                DataTable dt = system.GetList("loginTime > '" + DateTime.Parse(time) + "' and loginTime < '" + DateTime.Parse(time1) + "'").Tables[0];
                dgvsystemnode.DataSource = dt;
            }
            else
            {
                DataTable dt = system.GetList("loginTime > '" + DateTime.Parse(time) + "' and loginTime < '" + DateTime.Parse(time1) + "' and username='" + comboBox1.Text + "'").Tables[0];
                dgvsystemnode.DataSource = dt;
            }           
            dgvsystemnode.AllowUserToAddRows = false;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定删除"+this.comboBox1.Text+"的系统日志？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dr == DialogResult.OK)
            {
                string time = dateTimePicker1.Text + " " + dateTimePicker2.Text;
                string time1 = dateTimePicker3.Text + " " + dateTimePicker4.Text;
                usersystemnode system = new usersystemnode();
                system.DeleteAll(" username='" + this.comboBox1.Text + "' ");
                DataTable dt = system.GetList("").Tables[0];
                dgvsystemnode.DataSource = dt;
            }
        
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }

        }
        int flag = 0;
        private void SystemNode_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ExportExcel("", dgvsystemnode);
        }

        public void ExportExcel(string fileName, DataGridView myDGV)
        {
            string saveFileName = "";
            //bool fileSaved = false;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xls";
            saveDialog.Filter = "Excel文件|*.xls";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return; //被点了取消 
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象，可能您的机子未安装Office.Excel组件");
                return;
            }
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
            int colIndex = 0;
            //写入标题
            for (int i = 0; i < myDGV.ColumnCount; i++)
            {
                if (myDGV.Columns[i].Visible)
                {
                    colIndex++;
                    worksheet.Cells[1, colIndex] = myDGV.Columns[i].HeaderText;
                    //  worksheet.Cells[1, i] = myDGV.Columns[i].HeaderText;
                }
            }
            //写入数值
            for (int r = 0; r < myDGV.Rows.Count; r++)
            {
                colIndex = 0;
                for (int i = 0; i < myDGV.ColumnCount; i++)
                {
                    if (myDGV.Columns[i].Visible)
                    {
                        colIndex++;
                        worksheet.Cells[r + 2, colIndex] = myDGV.Rows[r].Cells[i].Value;
                    }
                }
                System.Windows.Forms.Application.DoEvents();
            }
            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                    //fileSaved = true;
                }
                catch (Exception ex)
                {
                    //fileSaved = false;
                    MessageBox.Show("导出文件时出错,文件可能正被打开！/n" + ex.Message);
                }

            }
            xlApp.Quit();
            GC.Collect();//强行销毁 
            // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL

            MessageBox.Show(fileName + "导出成功", "系统提示", MessageBoxButtons.OK);
        }
    }
}
